package com.mall.shopping.services.homepage;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * @description: 主页显示
 * @author wxy
 * @date 2022-01-19 23:19
 * @version 1.0
 */
@Service
public class IHomeServiceImpl implements IHomeService {


    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelConverter panelConverter;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse homePageResponse = new HomePageResponse();
        PanelDto panelDto = new PanelDto();

        try {

            Set<Panel> panels = panelMapper.selectPanelAll();
            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItemList = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());

                panel.setPanelContentItems(panelContentItemList);
                //对象转化
                List<PanelContentItemDto> panelContentItems = contentConverter.panelContentItem2Dto(panelContentItemList);
                panelDto.setPanelContentItems(panelContentItems);

            }
            //对象转化
            Set<PanelDto> panelDtos = panelConverter.panelsDoToDto(panels);

            //执行成功
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            homePageResponse.setPanelContentItemDtos(panelDtos);
        }catch (Exception e){
            e.printStackTrace();
            homePageResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            homePageResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return homePageResponse;
    }
}
